<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Console | Node.js v6.9.1 Documentation</title>
  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:400,700,400italic">
  <link rel="stylesheet" href="assets/style.css">
  <link rel="stylesheet" href="assets/sh.css">
  <link rel="canonical" href="https://nodejs.org/api/console.html">
</head>
<body class="alt apidoc" id="api-section-console">
  <div id="content" class="clearfix">
    <div id="column2" class="interior">
      <div id="intro" class="interior">
        <a href="/" title="Go back to the home page">
          Node.js
        </a>
      </div>
      <ul>
<li><a class="nav-documentation" href="documentation.html">About these Docs</a></li>
<li><a class="nav-synopsis" href="synopsis.html">Usage &amp; Example</a></li>
</ul>
<div class="line"></div>

<ul>
<li><a class="nav-assert" href="assert.html">Assertion Testing</a></li>
<li><a class="nav-buffer" href="buffer.html">Buffer</a></li>
<li><a class="nav-addons" href="addons.html">C/C++ Addons</a></li>
<li><a class="nav-child_process" href="child_process.html">Child Processes</a></li>
<li><a class="nav-cluster" href="cluster.html">Cluster</a></li>
<li><a class="nav-cli" href="cli.html">Command Line Options</a></li>
<li><a class="nav-console active" href="console.html">Console</a></li>
<li><a class="nav-crypto" href="crypto.html">Crypto</a></li>
<li><a class="nav-debugger" href="debugger.html">Debugger</a></li>
<li><a class="nav-dns" href="dns.html">DNS</a></li>
<li><a class="nav-domain" href="domain.html">Domain</a></li>
<li><a class="nav-errors" href="errors.html">Errors</a></li>
<li><a class="nav-events" href="events.html">Events</a></li>
<li><a class="nav-fs" href="fs.html">File System</a></li>
<li><a class="nav-globals" href="globals.html">Globals</a></li>
<li><a class="nav-http" href="http.html">HTTP</a></li>
<li><a class="nav-https" href="https.html">HTTPS</a></li>
<li><a class="nav-modules" href="modules.html">Modules</a></li>
<li><a class="nav-net" href="net.html">Net</a></li>
<li><a class="nav-os" href="os.html">OS</a></li>
<li><a class="nav-path" href="path.html">Path</a></li>
<li><a class="nav-process" href="process.html">Process</a></li>
<li><a class="nav-punycode" href="punycode.html">Punycode</a></li>
<li><a class="nav-querystring" href="querystring.html">Query Strings</a></li>
<li><a class="nav-readline" href="readline.html">Readline</a></li>
<li><a class="nav-repl" href="repl.html">REPL</a></li>
<li><a class="nav-stream" href="stream.html">Stream</a></li>
<li><a class="nav-string_decoder" href="string_decoder.html">String Decoder</a></li>
<li><a class="nav-timers" href="timers.html">Timers</a></li>
<li><a class="nav-tls" href="tls.html">TLS/SSL</a></li>
<li><a class="nav-tty" href="tty.html">TTY</a></li>
<li><a class="nav-dgram" href="dgram.html">UDP/Datagram</a></li>
<li><a class="nav-url" href="url.html">URL</a></li>
<li><a class="nav-util" href="util.html">Utilities</a></li>
<li><a class="nav-v8" href="v8.html">V8</a></li>
<li><a class="nav-vm" href="vm.html">VM</a></li>
<li><a class="nav-zlib" href="zlib.html">ZLIB</a></li>
</ul>
<div class="line"></div>

<ul>
<li><a class="nav-https-github-com-nodejs-node" href="https://github.com/nodejs/node">GitHub Repo &amp; Issue Tracker</a></li>
<li><a class="nav-http-groups-google-com-group-nodejs" href="http://groups.google.com/group/nodejs">Mailing List</a></li>
</ul>

    </div>

    <div id="column1" data-id="console" class="interior">
      <header>
        <h1>Node.js v6.9.1 Documentation</h1>
        <div id="gtoc">
          <p>
            <a href="index.html" name="toc">Index</a> |
            <a href="all.html">View on single page</a> |
            <a href="console.json">View as JSON</a>
          </p>
        </div>
        <hr>
      </header>

      <div id="toc">
        <h2>Table of Contents</h2>
        <ul>
<li><a href="#console_console">Console</a><ul>
<li><a href="#console_asynchronous_vs_synchronous_consoles">Asynchronous vs Synchronous Consoles</a></li>
<li><a href="#console_class_console">Class: Console</a><ul>
<li><a href="#console_new_console_stdout_stderr">new Console(stdout[, stderr])</a></li>
<li><a href="#console_console_assert_value_message_args">console.assert(value[, message][, ...args])</a></li>
<li><a href="#console_console_dir_obj_options">console.dir(obj[, options])</a></li>
<li><a href="#console_console_error_data_args">console.error([data][, ...args])</a></li>
<li><a href="#console_console_info_data_args">console.info([data][, ...args])</a></li>
<li><a href="#console_console_log_data_args">console.log([data][, ...args])</a></li>
<li><a href="#console_console_time_label">console.time(label)</a></li>
<li><a href="#console_console_timeend_label">console.timeEnd(label)</a></li>
<li><a href="#console_console_trace_message_args">console.trace(message[, ...args])</a></li>
<li><a href="#console_console_warn_data_args">console.warn([data][, ...args])</a></li>
</ul>
</li>
</ul>
</li>
</ul>

      </div>

      <div id="apicontent">
        <h1>Console<span><a class="mark" href="#console_console" id="console_console">#</a></span></h1>
<pre class="api_stability api_stability_2">Stability: 2 - Stable</pre><p>The <code>console</code> module provides a simple debugging console that is similar to the
JavaScript console mechanism provided by web browsers.</p>
<p>The module exports two specific components:</p>
<ul>
<li>A <code>Console</code> class with methods such as <code>console.log()</code>, <code>console.error()</code> and
<code>console.warn()</code> that can be used to write to any Node.js stream.</li>
<li>A global <code>console</code> instance configured to write to <code>stdout</code> and <code>stderr</code>.
Because this object is global, it can be used without calling
<code>require(&#39;console&#39;)</code>.</li>
</ul>
<p>Example using the global <code>console</code>:</p>
<pre><code class="lang-js">console.log(&#39;hello world&#39;);
  // Prints: hello world, to stdout
console.log(&#39;hello %s&#39;, &#39;world&#39;);
  // Prints: hello world, to stdout
console.error(new Error(&#39;Whoops, something bad happened&#39;));
  // Prints: [Error: Whoops, something bad happened], to stderr

const name = &#39;Will Robinson&#39;;
console.warn(`Danger ${name}! Danger!`);
  // Prints: Danger Will Robinson! Danger!, to stderr
</code></pre>
<p>Example using the <code>Console</code> class:</p>
<pre><code class="lang-js">const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);

myConsole.log(&#39;hello world&#39;);
  // Prints: hello world, to out
myConsole.log(&#39;hello %s&#39;, &#39;world&#39;);
  // Prints: hello world, to out
myConsole.error(new Error(&#39;Whoops, something bad happened&#39;));
  // Prints: [Error: Whoops, something bad happened], to err

const name = &#39;Will Robinson&#39;;
myConsole.warn(`Danger ${name}! Danger!`);
  // Prints: Danger Will Robinson! Danger!, to err
</code></pre>
<p>While the API for the <code>Console</code> class is designed fundamentally around the
browser <code>console</code> object, the <code>Console</code> in Node.js is <em>not</em> intended to
duplicate the browser&#39;s functionality exactly.</p>
<h2>Asynchronous vs Synchronous Consoles<span><a class="mark" href="#console_asynchronous_vs_synchronous_consoles" id="console_asynchronous_vs_synchronous_consoles">#</a></span></h2>
<p>The console functions are usually asynchronous unless the destination is a file.
Disks are fast and operating systems normally employ write-back caching;
it should be a very rare occurrence indeed that a write blocks, but it
is possible.</p>
<p>Additionally, console functions are blocking when outputting to TTYs
(terminals) on OS X as a workaround for the OS&#39;s very small, 1kb buffer size.
This is to prevent interleaving between <code>stdout</code> and <code>stderr</code>.</p>
<h2>Class: Console<span><a class="mark" href="#console_class_console" id="console_class_console">#</a></span></h2>
<!--type=class-->
<p>The <code>Console</code> class can be used to create a simple logger with configurable
output streams and can be accessed using either <code>require(&#39;console&#39;).Console</code>
or <code>console.Console</code>:</p>
<pre><code class="lang-js">const Console = require(&#39;console&#39;).Console;
const Console = console.Console;
</code></pre>
<h3>new Console(stdout[, stderr])<span><a class="mark" href="#console_new_console_stdout_stderr" id="console_new_console_stdout_stderr">#</a></span></h3>
<p>Creates a new <code>Console</code> by passing one or two writable stream instances.
<code>stdout</code> is a writable stream to print log or info output. <code>stderr</code>
is used for warning or error output. If <code>stderr</code> isn&#39;t passed, warning and error
output will be sent to <code>stdout</code>.</p>
<pre><code class="lang-js">const output = fs.createWriteStream(&#39;./stdout.log&#39;);
const errorOutput = fs.createWriteStream(&#39;./stderr.log&#39;);
// custom simple logger
const logger = new Console(output, errorOutput);
// use it like console
var count = 5;
logger.log(&#39;count: %d&#39;, count);
// in stdout.log: count 5
</code></pre>
<p>The global <code>console</code> is a special <code>Console</code> whose output is sent to
<a href="process.html#process_process_stdout"><code>process.stdout</code></a> and <a href="process.html#process_process_stderr"><code>process.stderr</code></a>. It is equivalent to calling:</p>
<pre><code class="lang-js">new Console(process.stdout, process.stderr);
</code></pre>
<h3>console.assert(value[, message][, ...args])<span><a class="mark" href="#console_console_assert_value_message_args" id="console_console_assert_value_message_args">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v0.1.101</span>
</div><p>A simple assertion test that verifies whether <code>value</code> is truthy. If it is not,
an <code>AssertionError</code> is thrown. If provided, the error <code>message</code> is formatted
using <a href="util.html#util_util_format_format_args"><code>util.format()</code></a> and used as the error message.</p>
<pre><code class="lang-js">console.assert(true, &#39;does nothing&#39;);
  // OK
console.assert(false, &#39;Whoops %s&#39;, &#39;didn\&#39;t work&#39;);
  // AssertionError: Whoops didn&#39;t work
</code></pre>
<p><em>Note: the <code>console.assert()</code> method is implemented differently in Node.js
than the <code>console.assert()</code> method <a href="https://developer.mozilla.org/en-US/docs/Web/API/console/assert">available in browsers</a>.</em></p>
<p>Specifically, in browsers, calling <code>console.assert()</code> with a falsy
assertion will cause the <code>message</code> to be printed to the console without
interrupting execution of subsequent code. In Node.js, however, a falsy
assertion will cause an <code>AssertionError</code> to be thrown.</p>
<p>Functionality approximating that implemented by browsers can be implemented
by extending Node.js&#39; <code>console</code> and overriding the <code>console.assert()</code> method.</p>
<p>In the following example, a simple module is created that extends and overrides
the default behavior of <code>console</code> in Node.js.</p>
<pre><code class="lang-js">&#39;use strict&#39;;

// Creates a simple extension of console with a
// new impl for assert without monkey-patching.
const myConsole = Object.setPrototypeOf({
  assert(assertion, message, ...args) {
    try {
      console.assert(assertion, message, ...args);
    } catch (err) {
      console.error(err.stack);
    }
  }
}, console);

module.exports = myConsole;
</code></pre>
<p>This can then be used as a direct replacement for the built in console:</p>
<pre><code class="lang-js">const console = require(&#39;./myConsole&#39;);
console.assert(false, &#39;this message will print, but no error thrown&#39;);
console.log(&#39;this will also print&#39;);
</code></pre>
<h3>console.dir(obj[, options])<span><a class="mark" href="#console_console_dir_obj_options" id="console_console_dir_obj_options">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v0.1.101</span>
</div><p>Uses <a href="util.html#util_util_inspect_object_options"><code>util.inspect()</code></a> on <code>obj</code> and prints the resulting string to <code>stdout</code>.
This function bypasses any custom <code>inspect()</code> function defined on <code>obj</code>. An
optional <code>options</code> object may be passed to alter certain aspects of the
formatted string:</p>
<ul>
<li><p><code>showHidden</code> - if <code>true</code> then the object&#39;s non-enumerable and symbol
properties will be shown too. Defaults to <code>false</code>.</p>
</li>
<li><p><code>depth</code> - tells <a href="util.html#util_util_inspect_object_options"><code>util.inspect()</code></a> how many times to recurse while
formatting the object. This is useful for inspecting large complicated objects.
Defaults to <code>2</code>. To make it recurse indefinitely, pass <code>null</code>.</p>
</li>
<li><p><code>colors</code> - if <code>true</code>, then the output will be styled with ANSI color codes.
Defaults to <code>false</code>. Colors are customizable; see
<a href="util.html#util_customizing_util_inspect_colors">customizing <code>util.inspect()</code> colors</a>.</p>
</li>
</ul>
<h3>console.error([data][, ...args])<span><a class="mark" href="#console_console_error_data_args" id="console_console_error_data_args">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v0.1.100</span>
</div><p>Prints to <code>stderr</code> with newline. Multiple arguments can be passed, with the
first used as the primary message and all additional used as substitution
values similar to <code>printf(3)</code> (the arguments are all passed to
<a href="util.html#util_util_format_format_args"><code>util.format()</code></a>).</p>
<pre><code class="lang-js">const code = 5;
console.error(&#39;error #%d&#39;, code);
  // Prints: error #5, to stderr
console.error(&#39;error&#39;, code);
  // Prints: error 5, to stderr
</code></pre>
<p>If formatting elements (e.g. <code>%d</code>) are not found in the first string then
<a href="util.html#util_util_inspect_object_options"><code>util.inspect()</code></a> is called on each argument and the resulting string
values are concatenated. See <a href="util.html#util_util_format_format_args"><code>util.format()</code></a> for more information.</p>
<h3>console.info([data][, ...args])<span><a class="mark" href="#console_console_info_data_args" id="console_console_info_data_args">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v0.1.100</span>
</div><p>The <code>console.info()</code> function is an alias for <a href="#console_console_log_data_args"><code>console.log()</code></a>.</p>
<h3>console.log([data][, ...args])<span><a class="mark" href="#console_console_log_data_args" id="console_console_log_data_args">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v0.1.100</span>
</div><p>Prints to <code>stdout</code> with newline. Multiple arguments can be passed, with the
first used as the primary message and all additional used as substitution
values similar to <code>printf(3)</code> (the arguments are all passed to
<a href="util.html#util_util_format_format_args"><code>util.format()</code></a>).</p>
<pre><code class="lang-js">var count = 5;
console.log(&#39;count: %d&#39;, count);
  // Prints: count: 5, to stdout
console.log(&#39;count:&#39;, count);
  // Prints: count: 5, to stdout
</code></pre>
<p>If formatting elements (e.g. <code>%d</code>) are not found in the first string then
<a href="util.html#util_util_inspect_object_options"><code>util.inspect()</code></a> is called on each argument and the resulting string
values are concatenated. See <a href="util.html#util_util_format_format_args"><code>util.format()</code></a> for more information.</p>
<h3>console.time(label)<span><a class="mark" href="#console_console_time_label" id="console_console_time_label">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v0.1.104</span>
</div><p>Starts a timer that can be used to compute the duration of an operation. Timers
are identified by a unique <code>label</code>. Use the same <code>label</code> when you call
<a href="#console_console_timeend_label"><code>console.timeEnd()</code></a> to stop the timer and output the elapsed time in
milliseconds to <code>stdout</code>. Timer durations are accurate to the sub-millisecond.</p>
<h3>console.timeEnd(label)<span><a class="mark" href="#console_console_timeend_label" id="console_console_timeend_label">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v0.1.104</span>
</div><p>Stops a timer that was previously started by calling <a href="#console_console_time_label"><code>console.time()</code></a> and
prints the result to <code>stdout</code>:</p>
<pre><code class="lang-js">console.time(&#39;100-elements&#39;);
for (var i = 0; i &lt; 100; i++) {
  ;
}
console.timeEnd(&#39;100-elements&#39;);
// prints 100-elements: 225.438ms
</code></pre>
<p><em>Note: As of Node.js v6.0.0, <code>console.timeEnd()</code> deletes the timer to avoid
leaking it. On older versions, the timer persisted. This allowed
<code>console.timeEnd()</code> to be called multiple times for the same label. This
functionality was unintended and is no longer supported.</em></p>
<h3>console.trace(message[, ...args])<span><a class="mark" href="#console_console_trace_message_args" id="console_console_trace_message_args">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v0.1.104</span>
</div><p>Prints to <code>stderr</code> the string <code>&#39;Trace :&#39;</code>, followed by the <a href="util.html#util_util_format_format_args"><code>util.format()</code></a>
formatted message and stack trace to the current position in the code.</p>
<pre><code class="lang-js">console.trace(&#39;Show me&#39;);
  // Prints: (stack trace will vary based on where trace is called)
  //  Trace: Show me
  //    at repl:2:9
  //    at REPLServer.defaultEval (repl.js:248:27)
  //    at bound (domain.js:287:14)
  //    at REPLServer.runBound [as eval] (domain.js:300:12)
  //    at REPLServer.&lt;anonymous&gt; (repl.js:412:12)
  //    at emitOne (events.js:82:20)
  //    at REPLServer.emit (events.js:169:7)
  //    at REPLServer.Interface._onLine (readline.js:210:10)
  //    at REPLServer.Interface._line (readline.js:549:8)
  //    at REPLServer.Interface._ttyWrite (readline.js:826:14)
</code></pre>
<h3>console.warn([data][, ...args])<span><a class="mark" href="#console_console_warn_data_args" id="console_console_warn_data_args">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v0.1.100</span>
</div><p>The <code>console.warn()</code> function is an alias for <a href="#console_console_error_data_args"><code>console.error()</code></a>.</p>

      </div>
    </div>
  </div>
  <script src="assets/sh_main.js"></script>
  <script src="assets/sh_javascript.min.js"></script>
  <script>highlight(undefined, undefined, 'pre');</script>
</body>
</html>

